home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / LEDIT108.ZIP / LEDLP / LEDLP_5A.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-15  |  9.1 KB  |  322 lines

  1. #include "ledit.h"
  2.  
  3. #define ID_CPP          101
  4. #define ID_PAS          102
  5. #define CPP_SYNTAX       27
  6. #define PAS_SYNTAX       28
  7. #define CPP_WORDS        11
  8. #define PAS_WORDS        21
  9. #define PAS_LEFT_BRACE   "begin"
  10. #define PAS_RIGHT_BRACE  "end"
  11.   // Actually "end" has multiple braces such as "case" or
  12.   // "record", but multiple braces aren't supported now
  13.   // Pascal source is written without such words and therefore
  14.   // Find Brace will work properly on it  
  15.  
  16. static MSG Msg;
  17. static char MsgBuf[2048];
  18. static HWND WndPAS;                   // Pascal source window
  19. static HWND WndCPP;                   // C++ source window
  20. static HINSTANCE hInst;
  21. static HWND Focus;
  22. static WORD CPPSyntax[CPP_SYNTAX] = { // Small subset of C++ syntax
  23.    '\"\\', // Quoting character
  24.    '\'\\', // Alternative quoting character
  25.    '//',   // Comment sign
  26.    '/*',   // Left comment sign
  27.    '*/',   // Right comment sign
  28.    '(',')','[',']','{', '}','+','-','*','/','\\',',',';','=',':',
  29.    '%','|','!','&','||','&&','=='
  30.   };
  31. static WORD PASSyntax[PAS_SYNTAX] = { // Small subset of Pascal syntax
  32.    '\'\'', // Quoting character
  33.    0x101,  // Alternative quoting character
  34.    0x101,  // Comment sign
  35.    '{',    // Left comment sign
  36.    '}',    // Right comment sign
  37.    '(',')','[',']','(*', '*)','+','-','*','/','\\',',',';','=',':',
  38.    ':=','<>','>=','<=','@','#','. ','..'
  39.   };
  40. static LPSTR CPPWords[CPP_WORDS] = {  // Small subset of C++ keywords
  41.   "switch","case","default","break","return","if","else","while","for",
  42.   "#define","#include"};
  43.  
  44. static LPSTR PASWords[PAS_WORDS] = {  // Small subset of Pascal keywords
  45.   "begin","end","case","break","exit","if","then","while","for","do",
  46.   "uses","type","const","var","function",
  47.   "or","and","div","mod","shl","shr"};
  48.  
  49.  
  50. // Main window function
  51. LRESULT CALLBACK _export FrameProc (HWND     Wnd,
  52.                     UINT     Msg,
  53.                     WORD     wParam,
  54.                     DWORD    lParam)
  55. {
  56.   switch (Msg)
  57.     {
  58.       case WM_CREATE:
  59.         {
  60.           // Create two LEdit children
  61.       WndCPP = CreateWindow("LEdit",
  62.                 "LEdit-File-Init-ledit_e4.cpp",
  63.                 WS_CHILD | WS_VISIBLE | ES_BIGINDENT
  64.                 | ES_SMALLSPACING | ES_HIGHLIGHT
  65.                 | WS_VSCROLL | WS_BORDER,
  66.                 0,0,0,0,
  67.                 Wnd,ID_CPP, hInst, NULL);
  68.       WndPAS = CreateWindow("LEdit",
  69.                 "LEdit-File-Init-ledit_e4.pas",
  70.                 WS_CHILD | WS_VISIBLE | ES_BIGINDENT
  71.                 | ES_SMALLSPACING | ES_HIGHLIGHT
  72.                 | WS_VSCROLL | WS_BORDER,
  73.                 0,0,0,0,
  74.                 Wnd,ID_PAS, hInst, NULL);
  75.  
  76.       // Choose first focused window
  77.       Focus = WndCPP;
  78.  
  79.       // Change a little LEdit behaviour
  80.       SendMessage(WndCPP,EM_SETFONT,
  81.         GetStockObject(DEVICE_DEFAULT_FONT),0L);
  82.       SendMessage(WndPAS,EM_SETFONT,
  83.         GetStockObject(DEVICE_DEFAULT_FONT),0L);
  84.  
  85.       // Set appropriate syntaxes
  86.       SendMessage(WndCPP,EM_SETSYNTAX,CPP_SYNTAX,(LONG) &CPPSyntax);
  87.       SendMessage(WndPAS,EM_SETSYNTAX,PAS_SYNTAX,(LONG) &PASSyntax);
  88.       break;
  89.         }
  90.       case WM_SIZE:
  91.     {
  92.       // Move LEdit windows
  93.       WORD Part1 = HIWORD(lParam) / 2;
  94.           WORD Part2 = Part1 + HIWORD(lParam) % 2;
  95.           MoveWindow(WndCPP,0,0,
  96.              LOWORD(lParam),Part1,FALSE);
  97.           MoveWindow(WndPAS,0,Part1,
  98.              LOWORD(lParam),Part2,FALSE);
  99.           return 0;
  100.     }
  101.       case WM_CTLCOLOR:
  102.     {
  103.           // Set default colors. Actually these are comment colors
  104.       SetBkColor(wParam,RGB(192,192,192));
  105.       SetTextColor(wParam,RGB(128,128,128));
  106.           return 0;
  107.         }
  108.       case EM_CTLCOLOREX:
  109.     {
  110.       switch ( (*(LPWORDDESC)lParam).wCode )
  111.         {
  112.           case WD_SIMPLEWORD:
  113.         {
  114.                   // Set default text color for words
  115.           SetTextColor(wParam,RGB(0,0,0));
  116.  
  117.                   // Mark C++ keywords if this is CPP window
  118.           if ( (*(LPWORDDESC)lParam).hwndSender == WndCPP )
  119.             {
  120.                       int i;
  121.               for (i = 0; i < CPP_WORDS; i++)
  122.             {
  123.               if ( lstrcmp((*(LPWORDDESC)lParam).caWord,
  124.                 CPPWords[i]) == 0)
  125.                 {
  126.                    SetTextColor(wParam,RGB(0,0,255));
  127.                                return 0;
  128.                             }
  129.             }
  130.             }
  131.  
  132.                   // Mark Pascal keyword if this is PAS window
  133.           else
  134.             {
  135.               int i;
  136.               for (i = 0; i < PAS_WORDS; i++)
  137.             {
  138.               if ( lstrcmpi((*(LPWORDDESC)lParam).caWord,
  139.                 PASWords[i]) == 0)
  140.                 {
  141.                    SetTextColor(wParam,RGB(0,0,255));
  142.                                return 0;
  143.                             }
  144.             }
  145.             } 
  146.           return 0;
  147.                 }
  148.           case WD_SYNTAXITEM:
  149.         {
  150.                   // Set text color for syntax words
  151.           SetTextColor(wParam,RGB(0,0,192));
  152.                   return 0;
  153.         }
  154.           case WD_BADLYQUOTEDTEXT:
  155.           case WD_QUOTEDTEXT:
  156.         {
  157.                   // Set text color for quoted text
  158.                   if ( HIWORD((*(LPWORDDESC)lParam).dwValue) )
  159.             SetTextColor(wParam,RGB(0,128,0));
  160.           else
  161.                     SetTextColor(wParam,RGB(255,0,0));
  162.                   return 0;
  163.         }
  164.           default:
  165.         // Set text color for the rest of words, that is
  166.                 // for numbers
  167.                 SetTextColor(wParam,RGB(0,128,0));
  168.             }
  169.           return 0;
  170.     }
  171.       case EM_WORDCLICK:
  172.         {
  173.       if ( lParam )
  174.         {
  175.               // Show message about word supplied if it exists
  176.           lstrcpy(MsgBuf,(*(LPWORDDESC)lParam).caWord);
  177.           lstrcat(MsgBuf,"\r\n");
  178.           if ( (*(LPWORDDESC)lParam).wCode == WD_SYNTAXITEM )
  179.         {
  180.                   WORD SyntaxIndex = HIWORD((*(LPWORDDESC)lParam).dwValue);
  181.           wvsprintf(MsgBuf+lstrlen(MsgBuf),"Syntax: %d",
  182.             &SyntaxIndex);
  183.         }
  184.           else if ( (*(LPWORDDESC)lParam).wCode & WDF_INTEGER )
  185.         {
  186.           wvsprintf(MsgBuf+lstrlen(MsgBuf),"Integer: %ld",
  187.             &(*(LPWORDDESC)lParam).dwValue);
  188.         }
  189.           MessageBox(Wnd,MsgBuf,"Click",MB_OK | MB_ICONINFORMATION);
  190.             }
  191.       else
  192.             // Show "White space" message
  193.             MessageBox(Wnd,"White space","Click",MB_OK | MB_ICONINFORMATION);
  194.           return 1;
  195.     }
  196.       case EM_FINDBRACE:
  197.     {
  198.       if ( (*(LPWORDDESC)lParam).wCode == WD_SYNTAXITEM )
  199.         {
  200.               // Indicate braces for parantheises, brackets etc.
  201.           WORD SyntaxIndex = HIWORD((*(LPWORDDESC)lParam).dwValue);
  202.               if ( SyntaxIndex > 10 )
  203.         return EMF_NOBRACE;
  204.               LRESULT LResult;
  205.           if ( SyntaxIndex % 2)
  206.                 {
  207.           SyntaxIndex++;
  208.           LResult = EMF_HASRIGHT;
  209.                 }
  210.           else
  211.                 {
  212.           SyntaxIndex--;
  213.           LResult = EMF_HASLEFT;
  214.                 }
  215.           (*(LPWORDDESC)lParam).dwValue = MAKELONG(0,SyntaxIndex);
  216.               return LResult;
  217.         }
  218.       else
  219.         {
  220.               // If this is CPP window then no more braces
  221.               if ( wParam == WndCPP )
  222.         return EMF_NOBRACE;
  223.  
  224.               // If this is PAS window then look for "begin"
  225.           if ( lstrcmpi( (*(LPWORDDESC)lParam).caWord,
  226.             PAS_LEFT_BRACE) == 0)
  227.                 {
  228.           lstrcpy( (*(LPWORDDESC)lParam).caWord, PAS_RIGHT_BRACE );
  229.                   return EMF_HASRIGHT | EMF_CASEINSENSITIVE;
  230.         }
  231.  
  232.               // Look also for "end"
  233.           if ( lstrcmpi( (*(LPWORDDESC)lParam).caWord,
  234.             PAS_RIGHT_BRACE) == 0)
  235.                 {
  236.           lstrcpy( (*(LPWORDDESC)lParam).caWord, PAS_LEFT_BRACE );
  237.                   return EMF_HASLEFT | EMF_CASEINSENSITIVE;
  238.         }
  239.         } 
  240.         }
  241.       case WM_COMMAND:
  242.     {
  243.           // Only LEdit notifications
  244.       switch ( HIWORD(lParam) )
  245.         {
  246.               // Store current focus
  247.           case EN_SETFOCUS:
  248.             Focus = LOWORD(lParam); 
  249.         }
  250.           return 0;
  251.     }
  252.       case WM_SETFOCUS:
  253.     {
  254.           // Set current focus
  255.           SetFocus(Focus);
  256.           return 0;
  257.         }
  258.       case WM_DESTROY:
  259.     {
  260.           // Stop the application
  261.       PostQuitMessage(0);
  262.       break;
  263.         }
  264.     }
  265.   // Call DefWindowProc() for the rest of messages
  266.   return DefWindowProc(Wnd,Msg,wParam,lParam);
  267. }
  268.  
  269. // Main Windows function
  270. #pragma argsused
  271. int PASCAL WinMain (HINSTANCE hInstance,
  272.             HINSTANCE hPrevInst,
  273.             LPSTR     lpszCmdLine,
  274.             int       nCmdShow)
  275.  
  276. {
  277.   // To ensure static loading of LEDIT.DLL
  278.   LVer();
  279.  
  280.   // Don't allow multiple instances
  281.   if ( hPrevInst != 0 )
  282.     return 0;
  283.  
  284.   // Set constant
  285.   hInst = hInstance;
  286.  
  287.   // Register window class
  288.   WNDCLASS wc;
  289.   wc.style         = CS_HREDRAW | CS_VREDRAW;
  290.   wc.lpfnWndProc   = (WNDPROC) FrameProc;
  291.   wc.cbClsExtra    = 0;
  292.   wc.cbWndExtra    = 0;
  293.   wc.hInstance     = hInstance;
  294.   wc.hIcon         = LoadIcon(0,IDI_APPLICATION);
  295.   wc.hCursor       = LoadCursor(0,IDC_ARROW);
  296.   wc.hbrBackground = (HBRUSH) (COLOR_APPWORKSPACE + 1);
  297.   wc.lpszMenuName  = 0;
  298.   wc.lpszClassName = "LEditFrame";
  299.   RegisterClass(&wc);
  300.  
  301.   // Create main window
  302.   HWND Wnd = CreateWindow("LEditFrame",
  303.               "C++ vs Pascal",
  304.               WS_OVERLAPPEDWINDOW,
  305.               0, 0,
  306.               GetSystemMetrics(SM_CXSCREEN),
  307.               GetSystemMetrics(SM_CYSCREEN),
  308.               0, 0, hInstance, NULL);
  309.   if ( Wnd )
  310.     {
  311.       // Show window
  312.       ShowWindow(Wnd,SW_SHOW);
  313.  
  314.       // Run message loop
  315.       while ( GetMessage(&Msg,0,0,0) )
  316.         {
  317.           TranslateMessage(&Msg);
  318.           DispatchMessage(&Msg);
  319.         }
  320.     }
  321. return 0;
  322. }